/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import com.xy.biz.fd.domain.req.documentShare.ListDocumentShareRequest;
import com.xy.biz.fd.domain.req.documentShare.SaveDocumentShareRequest;
import com.xy.lang.util.CalendarUtils;
import com.xy.tool.context.UserContextHolder;
import com.xy.tool.util.ConvertSizeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.fd.domain.resp.GetDocumentShareResponse;
import com.xy.biz.fd.domain.req.documentShare.GetDocumentShareRequest;
import com.xy.biz.fd.domain.query.DocumentShareQuery;
import com.xy.biz.fd.domain.converter.DocumentShareConverter;
import com.xy.biz.fd.client.DocumentShareClient;
import com.xy.biz.fd.service.DocumentShareService;

/**
 *
 *
 * @author wcs
 * @date 2021-2-8
 */
@Api(tags = "文档分享")
@Slf4j
@RestController
@RequestMapping("/api/fd/document_share")
public class DocumentShareController implements DocumentShareClient {

    @Autowired
    private DocumentShareService documentShareService;

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveDocumentShare(@RequestBody SaveDocumentShareRequest request) {
        return documentShareService.saveDocumentShare(request);
    }

//	@Validatable(groups = {SaveChecks.class})
//	@PostMapping(value = "/save_all")
//	public Result<?> saveAllDocumentShare(@RequestBody Collection<SaveDocumentShareRequest> collection) {
//		documentShareService.saveAllDocumentShare(DocumentShareConverter.toBO(collection));
//		return Result.success();
//	}

//	@Validatable(groups = {UpdateChecks.class})
//	@PostMapping(value = "/update")
//	public Result<?> updateDocumentShare(@RequestBody SaveDocumentShareRequest request) {
//		documentShareService.updateDocumentShare(DocumentShareConverter.toBO(request));
//		return Result.success();
//	}

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteDocumentShare(@RequestBody GetDocumentShareRequest request) {
        documentShareService.deleteDocumentShare(request.getDocumentShareId());
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/rel_delete")
    public Result<?> relDeleteDocumentShare(@RequestBody GetDocumentShareRequest request) {
        documentShareService.relDeleteDocumentShare(request.getDocumentShareId());
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetDocumentShareResponse> getDocumentShare(@RequestBody GetDocumentShareRequest request) {
        return documentShareService.getDocumentShare(request.getDocumentShareId());
    }

//	@PostMapping(value = "/list_by_ids")
//	@Override
//	public Result<List<GetDocumentShareResponse>> listDocumentShareById(@RequestBody Collection<Long> collection) {
//		return Result.success(DocumentShareConverter.toResponse(documentShareService.listDocumentShareById(collection)));
//	}
//
//	@PostMapping(value = "/list")
//	@Override
//	public Result<List<GetDocumentShareResponse>> listDocumentShare(@RequestBody ListDocumentShareRequest request) {
//		return Result.success(DocumentShareConverter.toResponse(documentShareService.listDocumentShare(DocumentShareConverter.toQuery(request))));
//	}
//
//	@PostMapping(value = "/count")
//	@Override
//	public Result<Integer> countDocumentShare(@RequestBody ListDocumentShareRequest request) {
//		return Result.success(documentShareService.countDocumentShare(DocumentShareConverter.toQuery(request)));
//	}

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetDocumentShareResponse>> pageDocumentShare(@RequestBody ListDocumentShareRequest request) {
        DocumentShareQuery query = DocumentShareConverter.toQuery(request);
        Long userId = UserContextHolder.getUserId();
        if (query.getShareDirection() == 0) {// 0代表我的分享
            query.setCreateUserId(UserContextHolder.getUserId());
            query.setShareTargetId(request.getId());
        } else if (query.getShareDirection() == 1) {// 1代表分享我的
            query.setShareTargetId(userId);
            query.setCreateUserId(request.getId());
        }
        List<GetDocumentShareResponse> list = documentShareService.listDocumentShare(query);
        list = list.stream()
                .peek(resp -> resp.setAccessTimeString(CalendarUtils.getDeadlineString(resp.getCreateTime(), LocalDateTime.now())))
                .collect(Collectors.toList());
        int count = documentShareService.countDocumentShare(query);
        return Result.success(new Page<>(list, count));
    }
}
